import widget_base
from widget_base import Template

WidgetList = ['TitleBar', 'TextBox', 'CodeBox']

class TitleBar(widget_base.NodeBase):
    ContentTemplate = """<h1 xmlns:py="http://purl.org/kid/ns#" py:content="Title">
       Title 
    </h1>
    """
    
    FormTemplate = """<span xmlns:py="http://purl.org/kid/ns#">
        <input type="text" value="${Title}" id="edit${ID}" />
        <a href="javascript: LoadCommandResult('modify', {'id': '${ID}', 'tocallback_prefix': 'content', 'modify_title': document.getElementById('edit${ID}').value});">Modify</a>
        |
        <a href="javascript: LoadCommandResult('cancel', {'id': '${ID}', 'tocallback_prefix': 'content'});">Cancel</a>
    </span>
    """
    
    def __init__(self):
        widget_base.NodeBase.__init__(self)
        self.Data['title'] = 'Title';
        
    @Template(source = ContentTemplate)
    def RenderContent(self, DisplayEditable = False):
        return dict(Title = self.Data['title'])
    
    @Template(source = FormTemplate)
    def RenderForm(self):
        return dict(Title = self.Data['title'], ID = self.ID)
    
    def Modify(self, *Args, **Kwargs):
        self.Data['title'] = Kwargs['title']
        
class TextBox(widget_base.NodeBase):
    ContentTemplate = """<span xmlns:py="http://purl.org/kid/ns#" py:content="XML(Content)">
        Content
    </span>
    """
    
    FormTemplate = """<span xmlns:py="http://purl.org/kid/ns#">
        <textarea name="textbox${ID}" id="textbox${ID}" cols="80" rows="10" py:content="Content"/>
        <br />
        <a href="javascript: LoadCommandResultWithPost('modify', {'id': '${ID}', 'tocallback_prefix': 'content', 'modify_content': tinyMCE.get('textbox${ID}').getContent()});">Modify</a>
        |
        <a href="javascript: LoadCommandResult('cancel', {'id': '${ID}', 'tocallback_prefix': 'content'});">Cancel</a>   
        <script language="javascript">
            InitTinyMCE('textbox${ID}');
        </script>
    </span>
    """
    
    def __init__(self):
        widget_base.NodeBase.__init__(self)
        self.Data['content'] = 'content';
        
    @Template(source = ContentTemplate)
    def RenderContent(self, DisplayEditable = False):
        return dict(Content = self.Data['content'])
    
    @Template(source = FormTemplate)
    def RenderForm(self):
        return dict(Content = self.Data['content'], ID = self.ID)
    
    @staticmethod
    def RenderHeader(DisplayEditable = False):
        if DisplayEditable:
            import turbogears
            Result = """<script type="text/javascript" src="%s"></script>\n""" % turbogears.url('/static/tiny_mce/tiny_mce.js')
            Result += """<script type="text/javascript" src="%s"></script>\n""" % turbogears.url('/static/javascript/widget/standard.js')
            return Result
        return ''
    
    def Modify(self, *Args, **Kwargs):
        self.Data['content'] = Kwargs['content']
        
class CodeBox(widget_base.NodeBase):
    ContentTemplate = """
    <div xmlns:py="http://purl.org/kid/ns#">
        <textarea py:content="Code" name="codebox${ID}" class="${Class}">
            Code
        </textarea>
        <script language="javascript">
            dp.SyntaxHighlighter.ClipboardSwf = '/static/dp.SyntaxHighlighter/Scripts/clipboard.swf';
            dp.SyntaxHighlighter.HighlightAll('codebox${ID}');
        </script>
    </div>
    """
    
    FormTemplate = """<span xmlns:py="http://purl.org/kid/ns#">
        <select id="class_select${ID}">
            <option py:for="ClassKey in ClassList" value="${ClassKey}" py:attrs="dict(selected='selected') if ClassKey == Class else {}">${ClassList[ClassKey]}</option>
        </select>
        <textarea name="codebox${ID}" id="codebox${ID}" cols="80" rows="10" py:content="Code"/>
        <br />
        <a href="javascript: LoadCommandResultWithPost('modify', {'id': '${ID}', 'tocallback_prefix': 'content', 'modify_code': document.getElementById('codebox${ID}').value, 'modify_class': document.getElementById('class_select${ID}').value});">Modify</a>
        |
        <a href="javascript: LoadCommandResult('cancel', {'id': '${ID}', 'tocallback_prefix': 'content'});">Cancel</a>   
    </span>
    """
    
    ClassList = {'cpp': 'C++',
                 'c-sharp': 'C#',
                 'css': 'CSS',
                 'delphi': 'Delphi',
                 'java': 'Java',
                 'javascript': 'Java Script',
                 'php': 'PHP',
                 'python': 'Python',
                 'ruby': 'Ruby',
                 'sql': 'Sql',
                 'vb': 'VB',
                 'xml': 'XML/HTML'
                 }
    
    JavaScriptList = ['/static/dp.SyntaxHighlighter/Scripts/shCore.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushCpp.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushCss.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushPhp.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushRuby.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushVb.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushCSharp.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushDelphi.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushJScript.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushPython.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushSql.js',
                    '/static/dp.SyntaxHighlighter/Scripts/shBrushXml.js'
                      ]
    
    DefaultClass = 'cpp'
    
    def __init__(self):
        widget_base.NodeBase.__init__(self)
        self.Data['code'] = '';
        self.Data['class'] = self.DefaultClass;

    @Template(source = ContentTemplate)
    def RenderContent(self, DisplayEditable = False):
        return dict(Code = self.Data['code'], Class=self.Data['class'], ID = self.ID)
    
    @Template(source = FormTemplate)
    def RenderForm(self):
        return dict(Code = self.Data['code'], Class=self.Data['class'], ClassList=self.ClassList, ID=self.ID)
    
    @staticmethod
    def RenderHeader(DisplayEditable = False):
        import turbogears
        Result = """<link type="text/css" rel="stylesheet" href="%(Url)s"></link>\n""" % dict(Url=turbogears.url('/static/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css'))
        for Url in CodeBox.JavaScriptList:
            Result += """<script language="javascript" src="%(Url)s"></script>\n""" % dict(Url=turbogears.url(Url))
        return Result
    
    def Modify(self, *Args, **Kwargs):
        self.Data['code'] = Kwargs['code']
        self.Data['class'] = Kwargs['class']